const checkBind = async (userInfo,deviceId,bind)=>{
		//绑定小程序与设备关系。
			if (userInfo.bindStatus) {
				return {
					status:0,
					code:0,
					data: {
						bindStatus: true
					}
				}
			} else {
				return	bind(deviceId)
			}	
}

const createBLEConnection =(dev,success,fail,onBLEConnectionStateChange,blood)=>{
	 
		//设备绑定成功  连接蓝牙
		// console.log('fslfjljsfp',blood)
		// success({
		// 				status: 0,
		// 				code:8,
		// 				data: {},
		// 				message:'测试99999999999'
		// })
		// return
		// console.log(dev)
		uni.createBLEConnection({
			//这个deviceId不能删除，否则会报错【从这里往后要放一放】		
			deviceId: dev.deviceId,
			success: (res) => {
				// console.log(99)

				// success({
				// 	status: 1,
				// 	code:2000,
				// 	data:{connected:true},
				// 	message:'连接设备成功'
				// })	 
				// console.log("设备已经连接(createBLEConnection)");
				// console.log('连接时获取设备id', this.deviceId);
				setTimeout(() => { 
					uni.getBLEDeviceServices({
						deviceId: dev.deviceId,
						success: (res) => {
							// for (let i = 0; i < res.services.length; i++) {
							// 	if (res.services[i].isPrimary) {
							// 		//将服务id存储添加到全局服务id数组中
							// 		this.serviceId=res.services[i].uuid;
							// 		console.log(`服务id:${i}`,this.serviceId);
							// 		this.getBLEDeviceCharacteristics(deviceId, this.serviceId)//获取蓝牙设备某个服务中所有特征值	
							// 		return
							// 	}
							// }
							// success({
							// 	status:1,
							// 	code:30,
							// 	data:{},
							// 	message:'蓝牙连接成功'
							// })
							//serviceId固定，获取蓝牙设备某个服务中所有特征值【读写属性的】
							let extParam = res
							blood.getBLEDeviceCharacteristics(dev,success,extParam)
						},
						fail: (err) => {
							fail({
								status:0,
								code:10,
								data:{connected:false},
								message:'连接蓝牙设备失败'
							})
							return
						}
					})
				}, 1000); //这里必须要延迟才行这是一个bug
			},
			fail: (err) => {
				
				uni.stopBluetoothDevicesDiscovery(); //此时停止蓝牙搜索
				fail({
						status:0,
						code:10,
						data:{connected:false},
						message:err.errMsg
				})
			}
		})
		
		
		uni.onBLEConnectionStateChange((res) => {
			onBLEConnectionStateChange({
								status:1,
								code:0,
								data:res,
								message:'蓝牙连接成功'
							})
		})
		uni.stopBluetoothDevicesDiscovery(); //此时停止蓝牙搜索
	
}

//sdk蓝牙链接
const sdkCreateBLEConnection=(dev,success,fail,onBLEConnectionStateChange,blood)=>{
	blood.getBLEDeviceCharacteristics(dev,success,fail)
	 
	uni.onBLEConnectionStateChange((res) => {
		onBLEConnectionStateChange({
							status:1,
							code:0,
							data:res,
							message:'蓝牙连接成功'
						})
	})
	uni.stopBluetoothDevicesDiscovery(); //此时停止蓝牙搜索 
	 
}


const openBluetoothAdapter =async ()=>{
	const resp = await new Promise((resolve, reject) => {
		
		uni.getBluetoothAdapterState({
		  success: (res) => {
			  console.log(res)
		  	 resolve(true)
		  },
		  fail:(err)=> {
		    console.log(err)
			if(err.errCode==10000)
			{
				uni.openBluetoothAdapter({
							success: (res) => {
								console.log("开启蓝牙适配器成功(openBluetoothAdapter success)", res);
								//扫描附近开启蓝牙的设备
								// this.startBluetoothDevicesDiscovery();
								// uni.showToast({
								// 	title: "开始扫描设备",
								// 	icon: "success",
								// });
								resolve(true)
							},
							fail: (res) => {
								uni.showToast({
									title: "请开启蓝牙",
									icon: "none",
								});
								if (res.errCode === 10001) {
									//持续监听蓝牙适配器状态是否打开，如果打开则马上自动搜索蓝牙设备
									uni.onBluetoothAdapterStateChange(function(res) {
										//监听蓝牙适配器是否打开，若打开则自动搜索蓝牙设备(onBluetoothAdapterStateChange)
										if (res.available) {
											resolve(true)
										}
									});
								}
							},
				});
				
			}
		  }
		})
		
	});
	// console.log('openBluetoothAdapter')
	// console.log('openBluetoothAdapter',resp)
	return resp
}
export default {
	checkBind,
	createBLEConnection,
	openBluetoothAdapter,
	sdkCreateBLEConnection
}